<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="spring" 	uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="tiles" 	uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="c"		uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page language="java" pageEncoding="utf-8"%>

<script type="text/javascript">
$(document).ready(function(){
	setMenuSelection("contracte");
	
    $("#btContractList").kendoButton({icon: "search"});

    $("#btContractCard").kendoButton({icon: "plus"});
	
//     $("#btApplyFilter").kendoButton({icon: "tick"});

//     $("#btResetFilter").kendoButton({icon: "cancel"});

    
    var grid = $("#gridContractsList").kendoGrid({
    	dataSource : {
	            type     : "json",
	            transport: {
	            	read: {
	               	 url: "json/listContracts",
	               	 data: { listType: 1 }
	                }
	            },
	            schema   :{
	                model: {
	                	 id:"id",
	                     fields: {
	                    	 refStatusFk: 	{ type: "number" },
	                    	 contractNo: 	{ type: "string" },
	                    	 contractDate:	{ type: "string" },
	                    	 refDomainsDesc: 	{ type: "string" },
	                         refServicesDesc: 	{ type: "string" },
	                         contractFiscCode: 	{ type: "string" },
	                         contractPerson: 	{ type: "string" },	 
	                    	 amount: 			{ type: "number" },
	                    	 prepayment: 		{ type: "number" },
	                    	 dbt: 				{ type: "number" },
	                    	 paymentOptionsDesc: 	{ type: "string" }
	                    }
	                  }
					}
                  
               },
        columns: [  
				{field: "refStatusFk", 		title: "Status", 				width: 90, template: renderStatus},
				{
					field:"id",  							
					title: "#",
					width: 40,
					hidden: true
				},
          		{	
					field:"contractNo",  						
					title: "Numărul contractului",
					sortable: true,
					width: 100,  
 					template: renderContractNo
				},
                
                {field: "contractDate", 			title: "Data contractului", 	width: 100},
                {field: "refDomainsDesc", 			title: "Domeniu", 				width: 120},
                {field: "refServicesDesc", 			title: "Serviciu", 				width: 120},
                {field: "contractFiscCode", 		title: "Cod fiscal", 			width: 120},
                {field: "contractPerson", 			title: "Denumire client", 		width: 120},
                {field: "amount", 					title: "Suma totala", 			width: 100},
                {field: "prepayment", 				title: "Avans", 				width: 120},
                {field: "dbt", 				title: "Rămas spre achitare", 			width: 120},
                {field: "paymentOptionsDesc", 				title: "Achitare", 		width: 120},
//                {field: "updatedDate", 				title: "Data primirii", 				width: 100},
                
          ],
          height: 500,
          sortable: true,
          pageable: {
        	  pageSize: 10,
        	  pageSizes: [10, 20, 30],
        	  refresh: true
          },
          dataBound: function(e){
              var grid = this;
				var dataSource = this.dataSource;
              
				var state = kendo.stringify({
					page: dataSource.page(),
					pageSize: dataSource.pageSize(),
					sort: dataSource.sort(),
					group: dataSource.group(),
					filter: dataSource.filter()
				});
              
				$.cookie("gridContractsListState",state);
              if($.cookie('empRows')){
                  $.each(JSON.parse($.cookie('empRows')),function(){                        
                      var item = dataSource.get(this);
                      var row = grid.tbody.find('[data-uid='+item.uid+']');
                      row.addClass('k-state-selected');
                  })
              }
			},
          change:function(){
              var grid = this;
              var ids = grid.select().map(function(){
                 return grid.dataItem($(this)).id
              }).toArray();
              $.cookie('empRows',JSON.stringify(ids));                    
          }
    }).data("kendoGrid");     
 
    /* ---------------------------------------------------- */
	var state = JSON.parse($.cookie("gridContractsListState"));
	if(state){
		if(state.filter){
			parseFilterDates(state.filter, grid.dataSource.options.schema.model.fields);					
		}
        grid.dataSource.query(state);
	}
	else{
		grid.dataSource.read();
	}
	/* ---------------------------------------------------- */
	
//     var grid = $("#gridContractsList").data("kendoGrid");
//     var data = grid.dataSource.data();
//     $.each(data, function (i, row) {alert("вавывываы");
// //       if (row.ProductName == 'Apples')
// //          $('tr[data-uid="' + row.uid + '"] ').css("background-color", "green");
//     });
    
//         //var dataItem = gridContractsList.dataItem(this);
//         var data = $("#gridContractsList").dataSource.data();
//         $.each(data, function (i, row) {
// 			alert("вавывываы");
//         });
 
       
	function  renderContractNo (dataItem, b, c) {
		
		switch (dataItem.refDomainsFk)
		{
		case 1: return "<a href='adaugaContractFinBank?pId="+dataItem.id + "'>" + kendo.htmlEncode(dataItem.contractNo) + "</a>";
		case 2: return "<a href='adaugaContractMM?pId="+dataItem.id + "'>" + kendo.htmlEncode(dataItem.contractNo) + "</a>";
		case 3: return "<a href='adaugaContractImob?pId="+dataItem.id + "'>" + kendo.htmlEncode(dataItem.contractNo) + "</a>";
		case 4: return "<a href='adaugaContractAsig?pId="+dataItem.id + "'>" + kendo.htmlEncode(dataItem.contractNo) + "</a>";
		case 6: return "<a href='adaugaContractEvCont?pId="+dataItem.id + "'>" + kendo.htmlEncode(dataItem.contractNo) + "</a>";
		}
		
// 		if (dataItem.refDomainsFk==1){
// 		return "<a href='adaugaContractFinBank?pId="+dataItem.id + "'>" + kendo.htmlEncode(dataItem.contractNo) + "</a>";
// 		}
// 		else
// 		{
// 			return "<a href='adaugaContract?pId="+dataItem.id + "'>" + kendo.htmlEncode(dataItem.contractNo) + "</a>";
// 		}
	
	};    
    
	
	function  renderStatus (dataItem, b, c) {
		var important='';
		var r=${proxyCurrentUser.userRoleId};
		var img='<img   src="resources/images/important.png" height="16px" />';
		
		switch (r)
		{
		  case 2: if (dataItem.refStatusFk==1) {important=img;} 		//Consultant OS
		                    break;
		  case 3: if (dataItem.refStatusFk==2) {important=img;} 		//Personal
		                    break;
		  case 4: if (dataItem.refStatusFk==3 || dataItem.refStatusFk==6) {important=img;} 		//Consultant OT
		                    break;
		  default: important='';
		};
		
		return  kendo.htmlEncode(dataItem.refStatusDesc) + important;
	};  
	
// 	function  renderDebt (dataItem, b, c) {
		
// 		return kendo.htmlEncode(dataItem.amount-dataItem.prepayment);
		
// 	}; 
	
});

/* ---------------------------------------------------- */
function parseFilterDates(filter, fields){
	if(filter.filters){
		for(var i = 0; i < filter.filters.length; i++){
			parseFilterDates(filter.filters[i], fields);
		}
	}
	else{
		if(fields[filter.field].type == "date"){
			filter.value = kendo.parseDate(filter.value);
		}
	}
}
/* ---------------------------------------------------- */

// function btApplyClick(){
// 	$("#gridContractsList").data("kendoGrid").dataSource.read();
// 	$("#gridContractsList").data("kendoGrid").refresh();
// };

// function btResetClick(){
	
// };

</script>
<CENTER>
<p style="text-align: left;">
<!-- 	<button id="btContractList" type="button" onclick="javascript:window.open('vizualizeazaContracte', target='_self')";>Lista Contractelor</button> -->
<!-- 	<button id="btContractCard" type="button" onclick="javascript:window.open('adaugaContract', 		target='_self')";>Adaugă contract</button> -->
	<c:if test="${errorMessage != '' &&  errorMessage != '0'}">
		<span id="errorMessage" class="errorMessage"> ${errorMessage} </span>
	</c:if>
</p>
<!-- <div id="tabPersonsListFilters" style="text-align: left; font-size: 12px;" class="k-block k-shadow" ><div class="k-header">Parametrii de cautare</div> -->
<!-- 	<table style="width: 100%; border: 1px;" border="0" > -->
<!-- 			<tr> -->
<!-- 				<td style="width: 100px;">Nr. contract </td> -->
<!-- 				<td style="width: 150px;">Contract finalizat</td> -->
<!-- 				<td style="width: 150px;">Contract contractat</td> -->
<!-- 				<td>Creat</td> -->
<!-- 			</tr> -->
<!-- 			<tr> -->
<!-- 				<td><input id='fContractNr' 			name='fContractNr' 		type='text' value='' class="k-textbox" ></td> -->
<!-- 				<td><input id='fFinalizat' 				name='fFinalizat' 	    type='checkbox' ></td> -->
<!-- 				<td><input id='fContractat' 			name='fContractat' 		type='checkbox' ></td> -->
<!-- 				<td><input id='fDate' 					name='fDate'	type='text' value='' class="k-textbox" style="width: 100px;"></td> -->
<!-- 			</tr> -->
<!-- 		</table> -->
<!-- 		<p style="text-align: right;"> -->
<!-- 				<button id="btApplyFilter" type="button"	onclick="btApplyClick()" >Aplica</button> -->
<!-- 				<button id="btResetFilter" type="button" 	onclick="btResetClick()" >Curata</button>			 -->
<!-- 		</p>		 -->
<!-- </div> -->
<div id="gridContractsList"></div>

</center>




